package com.LostAndFound.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.LostAndFound.domain.Root;
import com.LostAndFound.service.RootListService;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class RootListServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	RootListService service = new RootListService();

	// 检查管理员的username是否已经被占用
	public void checkAddUsername(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String addUsername = request.getParameter("addUsername");
		boolean isUsed = false;
		isUsed = service.checkAddUsername(addUsername);
		if(isUsed) {
			response.getWriter().write("false");
		}else {
			response.getWriter().write("true");
		}
	}
	// 分页，列表展示
	public void getRootListForEasyUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 当前页
		int pageNumber = Integer.parseInt(request.getParameter("page"));
		// 每页总条数
		int pageSize = Integer.parseInt(request.getParameter("rows"));
		// 总记录数
		int total = service.getTotalRootForEasyUI();
		List<Root> rootList = service.getRootListForEasyUI(pageNumber,pageSize);
		
		// JsonParser ==> JsonElement ==> JsonArray ==> JsonObject
		// 在 JsonObject 下修改 管理员权限 性别，再重新定义成Json格式
		Gson gson = new Gson();
		String json = gson.toJson(rootList);
		JsonParser parser = new JsonParser();
		JsonElement jsonElement = parser.parse(json);
		JsonArray jsonArray = jsonElement.getAsJsonArray();
		for (int i = 0; i < jsonArray.size(); i++) {
			JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
			if(jsonObject.get("limitFlag").getAsString().equals("1")) {
				jsonObject.remove("limitFlag");
				jsonObject.addProperty("limitFlag", "普通管理员");
				jsonObject.addProperty("limitFlagNum", "1");
			}else {
				jsonObject.remove("limitFlag");
				jsonObject.addProperty("limitFlag", "超级管理员");
				jsonObject.addProperty("limitFlagNum", "0");
			}
			if(jsonObject.get("gender").getAsString().equals("1")) {
				jsonObject.remove("gender");
				jsonObject.addProperty("gender", "男");
				jsonObject.addProperty("genderNum", "1");
			}else {
				jsonObject.remove("gender");
				jsonObject.addProperty("gender", "女");
				jsonObject.addProperty("genderNum", "0");
			}
		}
		String rows = gson.toJson(jsonElement);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"total\":"+total+",\"rows\":"+rows+"}");
	}
	// 分页，删除管理员
	public void handleDeleteRootForEasyUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String rid = request.getParameter("rid");
		boolean success = service.handleDeleteRootForEasyUI(rid);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"success\":"+success+"}");
	}
	// 分页，添加管理员
	public void handleAddMangerEasyUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取数据
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String realname = request.getParameter("realname");
		String cellphone = request.getParameter("cellphone");
		Integer gender = Integer.parseInt(request.getParameter("gender"));
		/*Integer limitFlag = Integer.parseInt(request.getParameter("limitFlag"));*/
		String email = request.getParameter("email");
		String resume = request.getParameter("resume");
		// 封装数据
		Root root = new Root();
		root.setUsername(username);
		root.setPassword(password);
		root.setRealname(realname);
		root.setCellphone(cellphone);
		root.setGender(gender);
		root.setEmail(email);
		root.setlimitFlag(1);
		root.setResume(resume);
		// 传递到 service 层
		boolean success = service.handleAddMangerEasyUI(root);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"success\":"+success+"}");
	}
	public void handleEditManagerEasyUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取数据
		Integer rid = Integer.parseInt(request.getParameter("rid"));
		String password = request.getParameter("password");
		String realname = request.getParameter("realname");
		String cellphone = request.getParameter("cellphone");
		Integer gender = Integer.parseInt(request.getParameter("gender"));
		/*Integer limitFlag = Integer.parseInt(request.getParameter("limitFlag"));*/
		String email = request.getParameter("email");
		String resume = request.getParameter("resume");
		// 封装数据
		Root root = new Root();
		root.setRid(rid);
		root.setPassword(password);
		root.setRealname(realname);
		root.setCellphone(cellphone);
		root.setGender(gender);
		root.setEmail(email);
		root.setlimitFlag(1);
		root.setResume(resume);
		// 传递到 service 层
		boolean success = service.handleEditManagerEasyUI(root);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"success\":"+success+"}");
	}
}